/**
 * 下午1:53:47
 */
package com.itboye.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.sunsunhome.www.aq.R;
import com.itboye.sunsunhome.www.aq.base.MyApplication;

/**
 * @author lizhenya
 *
 * @类描述：接收网络状态的广播接收器
 */
public class NetBroadcasttReceiver extends BroadcastReceiver {
	private static final int PING_CODE = 1;
	private Context mCtx;
	private static int pingResult;
	private static Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == PING_CODE) {
				pingResult = msg.arg1;
			}
		};
	};

	@Override
	public void onReceive(Context context, Intent intent) {
		this.mCtx = context;
		String action = intent.getAction();
		if (null == action) {
			return;
		}
		if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			netState(context);
		}
	}

	/**
	 * @方法描述：判断网络状态
	 * @param context
	 */
	private void netState(Context context) {
		
		ConnectivityType connectedType = NetworkHelper
				.getConnectedType(context);
		requestPingResult();
		if (connectedType == ConnectivityType.OFFLINE) {
			OnToast("网络链接已断开",MyApplication.context);
			return;
		}
		if (connectedType == ConnectivityType.UNKNOWN
				&& pingResult == NetworkHelper.PING_SUCCESS) {
//			toast("未知网络");
			OnToast("未知网络",MyApplication.context);
			return;
		} 

		if (connectedType == ConnectivityType.MOBILE) {
//			toast("移动蜂窝网络");
			OnToast("移动数据流量",MyApplication.context);
			return;
		} 
		if (connectedType == ConnectivityType.WIFI
				&& pingResult == NetworkHelper.PING_SUCCESS) {
//			toast("wifi连接");
			OnToast("WIFI已连接",MyApplication.context);
			return;
		} else if (connectedType == ConnectivityType.WIFI
				&& pingResult != NetworkHelper.PING_FAILE) {
//			toast("当前网络不可用");
			OnToast("当前网络不可用",MyApplication.context);
			return;
		}
	}

	/**
	 * @方法描述：请求Ping网络IP的结果
	 * @author lizhenya
	 */
	private void requestPingResult() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				int ping = NetworkHelper.Ping("www.baidu.com");

				Message message = Message.obtain();
				message.what = PING_CODE;
				message.arg1 = ping;
				handler.sendMessage(message);
			}
		}).start();
	}

//	private void toast(CharSequence message) {
//		Toast.makeText(mCtx, message, Toast.LENGTH_SHORT).show();
//	}
	   private void OnToast(String message,Context context) {  
	        View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast, null);  
	        Toast toast = new Toast(context);  
	        toast.setView(toastRoot);  
	        TextView tv = (TextView) toastRoot.findViewById(R.id.toast_notice);  
	        tv.setText(message);  
	        toast.setGravity(Gravity.CENTER, 0, 0);
	        toast.show();  
	    }
}
